/*
 * @lc app=leetcode.cn id=881 lang=cpp
 *
 * [881] 救生艇
 */

// @lc code=start
class Solution
{
public:
  int numRescueBoats(vector<int>& people, int limit)
  {
    int n = people.size();
    sort(people.begin(), people.end());
    int L = 0, R = n - 1;
    int ans = n;
    while (L < R) {
      if (people[L] + people[R] <= limit) {
        ++L;
        --R;
        --ans;
      }
      else
        --R;
    }
    return ans;
  }
};
// @lc code=end
